<?php

/*
Copyright 2007,2008 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once('BH_Widget_Data.php');
require_once('BH_Widget.php');

class BH_Widget_Manager {
	var $errorMsg;
	private function add_widget($name, $class) {
		global $bhwd;
		
		if ($bhwd->find_widget($name) !== false) {
			$this->errorMsg = "'$name' alread exists";
			return false;
		}
		
		if (!$class) {
			$this->errorMsg="You must select a class";
			return false;
		}
		
		if (!class_exists($class)) {
			$this->errorMsg="class '$class' is unknown";
			return false;
		}
		
		$widget = $bhwd->create_widget($name, $class);

		$o = new $class;
		$o->set_widget($widget);
		
		register_sidebar_widget($name, array( $o, 'widget'));
		
		if ($o instanceof BH_Controllable_Widget) {
		  list($x, $y) = $bhwd->get_class($class);
			register_widget_control($name, array( $o, 'control'), $x, $y);
		}
		
		return true;
	}

	private function remove_widget($id) {
		global $bhwd;
		
		if (!($widget = $bhwd->get_widget($id))) {
			$this->errorMsg = "Don't know widget '$id'";
			return false;
		}
		
		$bhwd->set_widget_delete($id, true);
		
		unregister_sidebar_widget($widget->widget_name);
		unregister_widget_control($widget->widget_name);
		return true;
	}
	
	private function copy_widget($id, $target) {
		global $bhwd;
		
		if (!($w = $bhwd->get_widget($id))) {
			$this->errorMsg = "Don't know widget id '$id'";
			return false;
		}
		
		return $this->add_widget($target, $w->widget_class);
	}
	
	private function rename_widget($id, $name) {
		global $bhwd;
	
		if (!($widget = $bhwd->get_widget($id))) {
			$this->errorMsg = "Don't know widget '$id'";
			return false;
		}
		
		if ($bhwd->find_widget($name) !== false) {
			$this->errorMsg = "'$name' alread exists";
			return false;
		}
		
		$bhwd->set_widget_name($id, $name);
		
		return true;
	}
		
	
	function admin_head () {
		$bh_widgets_url = get_settings('siteurl');
		
		if(substr($bh_widgets_url, -1, 1) != '/') {
			$bh_widgets_url .= '/';
		}
			
		$bh_widgets_url .= 'wp-content/plugins/bh-widgets/';

		echo '<link rel="stylesheet" href="'.$bh_widgets_url.'/bh-widget-admin.css" type="text/css" />'."\n";
	}
	
	
	function admin_page() {
	
		global $wpdb, $bhwd;

		$submit = $_POST['submit'];
		$classes = (array) $_POST['widget_classes'];
		$names = (array) $_POST['widget_name'];
		$widget_ids = (array) $_POST['widget_ids'];
		$widget_deletes = (array) $_POST['widget_deletes'];
		$class = $_POST['class'];
		$name = $_POST['name'];
		
		if ($submit =="Update") {
		
			foreach($widget_ids as $x => $widget_id) {
				$bhwd->set_widget_name($widget_id, $names[$x]);
				$bhwd->set_widget_class($widget_id, $classes[$x]);
			}
			
			foreach($widget_deletes as $widget_id)
				$bhwd->set_widget_delete($widget_id, 1);
		
		} else if ($submit == 'Create New') {
			$this->add_widget($name, $class);
		
		} else if ($submit == 'Copy') {
			$this->copy_widget($object, $name);	
		}

		echo "<div id=\"bh-widgets-mgr\">\n";
		echo "<h2>BH Widgets Mgr</h2>\n";

		if ($this->errorMsg) {
			echo '<h3 style="color:red;">' . $this->errorMsg . '</h3>';
		}

		$widgets = $bhwd->get_widgets();
		$classes = array_keys($bhwd->get_classes());
		natcasesort($classes);
		
		echo "<div>\n";
		
		echo "<p>";
		echo "<span class=\"widget-name\">Name</span>";
		echo "<span class=\"widget-class\">Class</span>";
		echo "<span class=\"widget-delete\">Trash</span>";
		echo "</p><br/>\n";
		
		echo "<form method=\"POST\" action=\"\">\n";
		wp_nonce_field('update-themes');
		echo '<input type="hidden" name="page_options" value="page,widget_id,widget_name,widget_classes,widget_delete" />'."\n";
		echo '<input type="hidden" name="page" value="bh-widgets/BH_Widget_Manager.php" />' . "\n";

		if ($widgets) {
		
			uasort($widgets, create_function('$x,$y', 'return strcasecmp($x->widget_name, $y->widget_name);'));
			
			foreach ($widgets as $widget) {
			
				if ($widget->widget_delete)
					continue;
					
				echo "<p>\n";
				echo '<input name="widget_ids[]" type="hidden" value="'.$widget->widget_id.'"/>';
				echo "<span class=\"widget-name\"><input name=\"widget_name[]\" value=\"". $widget->widget_name ."\" /></span>";
				echo "<span class=\"widget-class\"><select name=\"widget_classes[]\">";

				foreach ($classes as $c) {
					echo "<option".(($c == $widget->widget_class) ? " selected " : "" ).">$c</option>\n";
				}
				
				echo '</select></span>';
			
				echo "<span class=\"widget-delete\"><input name=\"widget_deletes[]\" type=\"checkbox\" ". ($widget->widget_delete ? "checked" : '') ." value=\"". $widget->widget_id ."\"/></span>\n";
				
				echo "</p><br/>\n";
			}
		}
				
		echo "</div>\n";


		echo '<p style="line-height: 30px;">'. "\n";
		echo "<span class=\"submit\"><input name=\"submit\" type=\"submit\" value=\"". __('Update') ."\" /></span>\n";
		echo "</p>\n";


		echo '<p style="line-height: 30px;">'. "\n";
		
		if ($widgets) {
			echo '<select name="object" >'. "\n";
			echo '<option value="">select widget</option>'."\n";

			foreach ( $widgets as $id => $widget)  {
				if (!$id || !$widget)
					continue;
					
				if ($widget->widget_delete)
					continue;
					
				echo '<option value="'. $id .'">'. $widget->widget_name .'</option>'. "\n";
			}
			
			echo '</select>'. "\n";
			echo '<input type="text" name="new_name"/>'. "\n";
			echo '<span class="submit"><input type="submit" name="submit" id="submit" value="'. __('Copy') .'" /></span>'. "\n";
		}
		
		echo '</p>';
		echo '<p style="line-height: 30px;">'. "\n";
		
		

		if ($classes) {
			echo '<select name="class" >'. "\n";
			echo '<option value="">select class</option>'."\n";
	
			foreach ( $classes as $class )
				echo '<option value="'. $class .'">'. $class .'</option>'. "\n";
	
			echo '</select>'. "\n";
	
			echo '<input type="text" name="name"/>'. "\n";
			echo '<span class="submit"><input type="submit" name="submit" id="submit" value="'. __('Create New') .'" /></span>'. "\n";
		}
		
		echo '</p>';
		
		echo '<p style="line-height: 30px;">'. "\n";
		echo '</p>';
		
		echo '</form>'. "\n";

		echo "</div><!-- bh-widgets-mgr -->\n";
	}
	
	function admin_menu() {
		$page = add_theme_page('BH Widgets Mgr', 'BH Widgets Mgr', 8, __FILE__ ,array($this, 'admin_page'));
		add_action('admin_head-'. $page, array($this, 'admin_head'));
	}

	function widgets_init() {
		global $bhwd;
		
		if (!$bhwd) return;
		
		if ($bhwd->get_widgets())
			foreach ($bhwd->get_widgets() as $id => $widget)  {
				
				if (!$id)
					continue;
					
				$class = $widget->widget_class;
				
				if (!class_exists($class))
					continue;
					
				$o = new $class;
				
				if (!($o instanceof BH_Widget)) {
					$this->errorMsg = "$class is not an instance of BH_Widget";
					continue;
				}
				
				$o->set_widget($widget);
				
				register_sidebar_widget($widget->widget_name, array( $o, 'widget'));
				
				if (!($o instanceof BH_Controllable_Widget))
					continue;
					
				if (!(list($x, $y) = $bhwd->get_class($class)))
					continue;
					
				register_widget_control($widget->widget_name, array( $o, 'control'), $x, $y);
			}

		add_action('admin_menu', array($this, 'admin_menu'));
	}
}

?>
